# !/usr/bin/env python
# coding:utf-8

import os, time

print('before the fork,my PID is', os.getpid())

pid = os.fork()

if pid < 0:
    print("error in fork!\n")
elif pid == 0:
    # 等于0表示子进程
    print('son PID is', os.getpid())
    os.system('python3 main.py ./data/ip2region.db')
    time.sleep(3)
    exit(0)
else:
    # 父进程
    print("The parent process ID is ", os.getpid());
    while 1:
        # pid_w = waitpid(pid, NULL, WNOHANG);
        pid_w = os.waitpid(pid, 0)[0]
        if pid_w == pid:
            # 子进程结束
            print("Son ended,open other one!\n");
            pid = os.fork()
            if pid < 0:
                print("error in fork!\n")
            elif pid == 0:
                # 等于0表示子进程
                print('son PID is', os.getpid())
                os.system('python3 main.py ./data/ip2region.db')
                time.sleep(3)
                exit(0)
    time.sleep(3)
